package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.CardBinDao;
import com.jiepos.mpos.common.entity.App_Card_BinEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 卡bin表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月26日下午2:21:01
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class CardBinService extends BaseService {
	
	@Autowired
	private CardBinDao cardBinDao;
	
	/**
	 * 获取卡BIN列表
	 * @param pagination
	 * @param cardBin
	 * @return
	 */
	public Map getCardBinList(Pagination pagination,App_Card_BinEntity cardBin){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.card_bin_uuid as uuid,t1.card_bin,t1.card_name,t1.card_ins_cd,t1.card_ins_nm,t1.card_len,NVL2 ( t2.code_val||t2.code_desc, t2.code_val||'-'||t2.code_desc, '') as crd_nature from ");
		sb.append(Table.T_APP_CARD_BIN).append(" t1 left join ");
		sb.append(Table.T_APP_CODE_LIST);
		sb.append(" t2 on t1.crd_nature=t2.code_val and t2.code_tp='CRD_NATURE' ");
		sb.append(" where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(cardBin.getCard_bin())) {
			sb.append( " and t1.card_bin like ? " );
			list.add("%" + cardBin.getCard_bin() + "%");
		}
		if (!StringUtils.isBlank(cardBin.getCard_name())) {
			sb.append( " and t1.card_name like ? " );
			list.add("%" + cardBin.getCard_name() + "%");
		}
		if (!StringUtils.isBlank(cardBin.getCard_ins_cd())) {
			sb.append( " and t1.card_ins_cd like ? " );
			list.add("%" + cardBin.getCard_ins_cd() + "%");
		}
		if (!StringUtils.isBlank(cardBin.getCard_ins_nm())) {
			sb.append( " and t1.card_ins_nm like ? " );
			list.add("%" + cardBin.getCard_ins_nm() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return cardBinDao.getPaginationMap(pagination, "t1", list);	
	}
	
	/**
	 * 保存卡BIN
	 * @param cardBin
	 * @param map
	 */
	public void saveCardBin(App_Card_BinEntity cardBin,Map map){
		cardBin.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		if (!StringUtils.isBlank(cardBin.getCard_bin_uuid())) {
			App_Card_BinEntity newCardBin = cardBinDao.findByParam("card_bin_uuid", cardBin.getCard_bin_uuid());
			cardBin.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			cardBin.setRec_id(newCardBin.getRec_id());
			cardBin.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("card_bin_uuid");
			noChangeList.add("rec_crt_ts");
			cardBinDao.update(cardBin, noChangeList);
		}else{
			cardBin.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			cardBin.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			cardBin.setCard_bin_uuid(IDFactoryUtil.getUUID());			
			cardBin.setRec_st(SystemConstant.NORMAL_STATUS);
			cardBin.setLast_oper_in(SystemConstant.OPER_ADD);
			cardBinDao.save(cardBin);
		}		
	}
	
	/**
	 * 根据记录id查询实体对象
	 * @param card_bin_uuid
	 * @return
	 */
	public App_Card_BinEntity getCardBinById(String card_bin_uuid){
		if (StringUtils.isBlank(card_bin_uuid) || card_bin_uuid.equals("undefined")) {
			return new App_Card_BinEntity();
		}
		return cardBinDao.findByParam("card_bin_uuid", card_bin_uuid);
	}
	
	/**
	 * 根据记录id集合删除数据
	 * @param card_bin_uuids
	 */
	public void deleteCardBinByIds(String card_bin_uuids){
		cardBinDao.deleteByStatus("card_bin_uuid", card_bin_uuids);
	}
	
	/**
	 * 校验卡bin
	 * @param cardBin
	 * @return
	 */
	public String checkCardBin(App_Card_BinEntity cardBin){
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.card_bin_uuid,t1.card_bin,t1.card_name,t1.card_ins_cd,t1.card_ins_nm from ");		
		sb.append(Table.T_APP_CARD_BIN);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(cardBin.getCard_bin())) {
			sb.append( " and t1.card_bin = ? " );
			list.add(cardBin.getCard_bin());
		}			
		if (!StringUtils.isBlank(cardBin.getCard_bin_uuid())) {
			sb.append( " and t1.card_bin_uuid != ? " );
			list.add(cardBin.getCard_bin_uuid());
		}		
		List result=cardBinDao.queryForList(sb.toString(), list);
		if(result==null||result.size()==0){			
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}

}
